/**
 * Created by Administrator on 2016/1/7.信息的轮询
 */
(function($){
    $.extend($,{
        getRandomColor:function(){
           return "#"+(((Math.random()*16777215+0.5)>>0).toString(16)).slice(-6);
        },
    //轮询弹幕信息
        pollMsg : function(){
            var lastTimestamp = new Date().getTime();
            var endHandler = function(){

                $("#tempPlayer")[0].pause();
                $("#tempPlayer").attr("src","");
            };
            $("#tempPlayer").removeProp("loop");
            $("#tempPlayer")[0].addEventListener("ended",endHandler);
            window.msgTemp = setInterval(function(){
                $.post(command.cloudeServer+"message/getMessages",{
                    lastTimestamp:lastTimestamp
                },function(data){
                    lastTimestamp = data.lastTimestamp;
                    if(data.msgs && data.msgs.length!=0){
                        $.each(data.msgs,function(i,d){
                            var pos = Math.floor(Math.random()*15+1);
                            var suff = "";
                            for(var i=0;i<pos;i++){
                                suff += " ";
                            }
                            if(d.type==1){
                                damoo.emit({ text: d.message+suff, color: $.getRandomColor() ,shadow: { color: "#f49"} });
                            }else if(d.type==2){
                                $("#tempPlayer").attr("src","/audio/网络歌手-音效 鼓掌声 口哨.mp3");
                                $("#tempPlayer")[0].play();
                            }else if(d.type==3){
                                $("#tempPlayer").attr("src","/audio/网络歌手-鼓掌声 喝彩.mp3");
                                $("#tempPlayer")[0].play();
                            }
                        });
                    }else{
                        $("#tempPlayer")[0].pause();
                        $("#tempPlayer").attr("src","");
                    }
                })
            },5000);
        },
        //轮询签到信息
        signin:function(){
            var lastTimestamp = 1452149448653;
            //保存已签到
            var key = "signs";

            //轮询获取签到人
            window.signinTemp =  setInterval(function(){
                $.post(command.cloudeServer+"user/getSignInUser",{
                    lastTimestamp:lastTimestamp
                },function(data){
                    if(data.success){
                        var users = data.users;
                        if(users && users.length==0){
                            return ;
                        }
                        var localStr = localStorage.getItem(key);
                        //并且上墙
                        var wall = $("#signin-content","#signin_html");

                        var onUsers = [];
                        if(localStr){
                            //如果有数据则去重，再保存
                            var tempArr = localStr.split(",");
                            $.each(users,function(i,d){
                                if($.inArray(d,tempArr)==-1){
                                    tempArr.push(d);
                                    onUsers.push(d);
                                }
                            });
                            localStorage.setItem(key,tempArr);
                        }else{
                            //无数据直接将取回数据放入
                            localStorage.setItem(key,users);
                            onUsers = users;
                        }
                        $.each(users,function(i,d){
                            var html = [
                                '<div  class="signin-item signin-item-show" nm="'+d+'">',
                                '    <div class="signin-item-img ani"  swiper-animate-effect="flip" swiper-animate-duration="2.5s" swiper-animate-delay="0.3s">',
                                '        <img ',
                                    '        src="images/photo/'+ $.trim(d)+'.jpg">',
                                '    </div>',
                                    '    <span class="signin-item-text">'+d+'</span>',
                                '</div>  '
                            ].join("");
                            //展示上墙动画
                            if($("#signin_html").find(".signin-item[nm='"+d+"']").length==0){
                                wall.append(html);
                                damoo.emit({ text: "欢迎"+d+"成功签到，来到年会现场！！！！" });
                                swiperAnimate($("#signin_html").find(".signin-item:last")[0])
                            }
                        });
                        $(".signin-header-num").html($("#signin_html").find(".signin-item").length);
                        lastTimestamp = data.lastTimestamp;
                    }
                });
            },5000);
        }
    });
})(jQuery)